package demo1.web.servlet.userBuyapi;

import demo1.dao.Mapper.userBuyMapper;
import demo1.dao.sql.userBuysql;
import demo1.service.userBuyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Objects;

@WebServlet("/userBuyRegister")
public class userBuyRegister extends HttpServlet {//用户的注册
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");
        resp.setContentType("text/html;charset = utf-8");
        PrintWriter writer = resp.getWriter();
        try {
            userBuyService check = new userBuyService();
            int num = check.checkUserName(userName);
            if (num == 1) {
                //没有一样的用户名
                check.userBuyRegister(userName, userPwd);
                resp.sendRedirect("registerSuccess2.jsp");
            } else {
                //如果有一样的用户名
                writer.write("用户名重复了");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
